package com.jarvis.pzz.modules.shop.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jarvis.pzz.R;
import com.jarvis.pzz.models.ShopListModel;
import com.jarvis.pzz.modules.main.adapter.SignAdapter;
import com.jarvis.pzz.util.ViewHolderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jarvis.liu on 9/12/2016.
 */
public class ShopLikeAdapter extends BaseAdapter {
    private List<ShopListModel> list;
    private Context context;
    private SignAdapter adapter;

    public ShopLikeAdapter(List<ShopListModel> list, Context context) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ShopListModel shopListModel = list.get(position);
        convertView = LayoutInflater.from(context).inflate(R.layout.item_shop, parent, false);
        LinearLayout lin_sign = ViewHolderUtil.get(convertView, R.id.lin_sign);
        ImageView img = ViewHolderUtil.get(convertView, R.id.img);
        ImageView img_top = ViewHolderUtil.get(convertView, R.id.img_top);
        TextView tv_name = ViewHolderUtil.get(convertView, R.id.tv_name);
        TextView tv_demo = ViewHolderUtil.get(convertView, R.id.tv_demo);
        TextView tv_shopIntroduce = ViewHolderUtil.get(convertView, R.id.tv_shopIntroduce);
        LinearLayout lin_bg = ViewHolderUtil.get(convertView, R.id.lin_bg);
        TextView tv_shop_rent = ViewHolderUtil.get(convertView, R.id.tv_shop_rent);

        Glide.with(context).load(shopListModel.getShopThumbnailImgPath()).placeholder(R.drawable.img_default).error(R.drawable.img_default).into(img);
        tv_name.setText(shopListModel.getShopTitle());
        tv_shop_rent.setText(shopListModel.getShopRent());
        String sign = shopListModel.getShopLables();
        tv_shopIntroduce.setText(shopListModel.getShopIntroduce());
        lin_bg.setBackgroundColor(context.getResources().getColor(R.color.grey_2));
        if(TextUtils.isEmpty(shopListModel.getShopAuditingState())){
            img_top.setVisibility(View.GONE);
        }else {
            if("1".equals(shopListModel.getShopAuditingState())){
                img_top.setVisibility(View.VISIBLE);
            }else {
                img_top.setVisibility(View.GONE);
            }
        }
        if (!TextUtils.isEmpty(sign)) {
            String[] split = sign.split(",");
            List<String> list_sign = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                list_sign.add(split[i]);
            }
            lin_sign.removeAllViews();
            int count = list_sign.size();
            if (count > 3) {
                count = 3;
            }
            for (int i = 0; i < count; i++) {
                TextView tv2 = new TextView(context);
                tv2.setText(list_sign.get(i));
                tv2.setBackgroundColor(context.getResources().getColor(R.color.grey_6));
                tv2.setTextColor(context.getResources().getColor(R.color.grey_7));
                tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
                tv2.setGravity(Gravity.CENTER);
                tv2.setMaxLines(1);
                tv2.setEllipsize(TextUtils.TruncateAt.END);
                tv2.setPadding(3, 3, 3, 3);
                ViewGroup.LayoutParams layoutParams1 = tv_demo.getLayoutParams();
                tv2.setLayoutParams(layoutParams1);

                LinearLayout lin = new LinearLayout(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                if (i != 0) {
                    params.setMargins(10, 0, 0, 0);
                }
                lin.setLayoutParams(params);
                lin.addView(tv2);
                lin_sign.addView(lin);
            }
        }

//        adapter = new SignAdapter(list_sign, context);
//        sg_sign.setAdapter(adapter);
        return convertView;
    }
}
